package com.eastmacro.until;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GetFile {
	public static List<String> getFileList(String path, String suffix,
			boolean isdepth) {
		File file = new File(path);
		File[] fileName = file.listFiles();

		for (int i = 0; i < fileName.length; i++) {
			String filePath = fileName[i].getAbsolutePath();
			// System.out.println("filePath="+filePath);
			if (suffix != null) {
				if (filePath.endsWith(suffix)) {
					list.add(filePath);
				}
			}
		}
		return list;
	}

	public static String getTitle(String filename) {
		String title = "";
		try {
			BufferedReader br = new BufferedReader(new FileReader(filename));
			String line = "";
			int num = 0;
			while ((line = br.readLine()) != null) {
				if (num >= 37)
					break;
				else {
					num++;
					int temp = line.indexOf("Title: ");
					if (temp != -1) {
						title = line.substring(temp + 7);
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return title;
	}

	public static String getHead(String filename) {
		String head = "";
		try {
			BufferedReader br = new BufferedReader(new FileReader(filename));
			String line;
			int num = 0;
			boolean isFind = false;
			while ((line = br.readLine()) != null) {
				if (num == 36 || num == 37 ) {
					char[] tmp = line.trim().toCharArray();
					int i = 0;
					for (i = 0; i < tmp.length; i++) {
						if ((tmp[i] == ' ') || (tmp[i] > 127)){
							isFind = true;
							break;
						}
					}
					for (int j = 0; j < i; j++) {
						if((j==0)&&(tmp[j]!='H')&&(tmp[j]!='S')&&(tmp[j]!='s')&&(tmp[j]!='D')){
							isFind = true;
							break;
						}
						head = head + tmp[j];
					}
				}else if(num == 42){
					System.out.print(line);
					char[] tmp = line.toCharArray();
					int i = 0;
					//System.out.println(tmp.length);
					for (i = 0; i < tmp.length; i++) {
//						if ((tmp[i] == ' ') || (tmp[i] > 255)){
//							isFind = true;
//							break;
//						}
					}
					for (int j = 0; j < i; j++) {
						if((j==1)&&(tmp[j]!='H')&&(tmp[j]!='S')&&(tmp[j]!='s')&&(tmp[j]!='D')){
							isFind = true;
							break;
						}
						head = head + tmp[j];
					}
				}
				if(isFind) break;
				num++;
			}
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e2) {
			e2.printStackTrace();
		}
		return head;
	}

	private static List<String> list = new ArrayList<String>();
}
